package me.snowiest.snowdrift.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import me.snowiest.snowdrift.entity.Resource;
import me.snowiest.snowdrift.service.ResourceService;
import me.snowiest.snowdrift.vo.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@Api("资源接口")
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @GetMapping("{id}")
    public CommonResult getResourceById(@PathVariable("id") Integer resourceId) {
        Resource resource = resourceService.getById(resourceId);
        return CommonResult.success().data("resource", resource);
    }

    @GetMapping
    public CommonResult listResource(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize
    ) {
        Page<Resource> page = resourceService.page(new Page<>(pageNum, pageSize));
        return CommonResult.success().data("page", page.getRecords());
    }

    @PostMapping
    public CommonResult addResource(Resource resource) {
        System.out.println(resource);
        resourceService.save(resource);
        return CommonResult.success().data("resource", resource);
    }
}
